<!--
 * @Description: 用户列表
 * @Author: zhihong deng
 * @Date: 2021-07-28 23:04:43
 * @LastEditors: zhihong deng
 * @LastEditTime: 2021-07-29 15:01:43
-->
<template>
  <div>
    <el-input v-model="kw" placeholder="请输入查询关键字"></el-input>
    <el-button type="primary" @click="searchHanlder">查询</el-button>
    <el-button type="primary" @click="addHanlder">新增</el-button>
    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        prop="id"
        label="序号"
        width="80">
      </el-table-column>
      <el-table-column
        prop="username"
        label="用户名"
        width="100">
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱">
      </el-table-column>
      <el-table-column
        prop="mobile"
        label="手机号">
      </el-table-column>
      <el-table-column
        prop="role_name"
        label="角色名称">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="editHanlder(scope)">编辑</el-button>
          <el-button type="danger" @click="removeHanlder(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="pageSizes"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
   <!-- 添加用户弹框 -->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="用户名称" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" :label-width="formLabelWidth">
          <el-input type="password" v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="form.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="storeUser">添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {userList,userAdd,userDel} from '../../api/user'
export default {
  data () {
    return {
      kw: '',
      tableData: [],
      page: 1,
      size: 2,
      pageSizes: [2, 5, 10, 100,200],
      total: 200,
      dialogFormVisible: false,
      formLabelWidth: '100',
      form:{
        username:'',
        password:'',
        email:'',
        mobile:''
      }
    }
  },
  created () {
    this.getUserList();
  },
  methods:{
    // 查询关键字
    searchHanlder () {
      if(!this.kw){
        this.$message.error('请输入查询的关键字')
        return this.getUserList(1,this.size)
      }
      // 调用查询接口获取信息，有可能用户是在页码点击查询，所以要重置为第一页
      this.getUserList(1,this.size,this.kw)
    },
    // 获取用户信息
    getUserList (page = 1,size = 2,kw = '') {
      userList(page,size,kw).then( res => {
        let {meta,data} = res.data
        if(meta.status == 200){
           this.$message.success('获取用户信息成功！')
           this.total = data.total;
           this.tableData = data.users
        }else{
          this.$massage.error('获取用户信息失败！' + meta.msg)
        }
      }).catch( error => {
        this.$message.error('获取用户信息失败！')
      })
    },
    // 设置每页几条信息
    handleSizeChange(size) {
      this.size = size
      // console.log(`每页 ${size} 条`);
      this.getUserList(1,size)
    },
    // 当前是第几页
    handleCurrentChange(page) {
      this.page = page
      console.log(`当前页:`,page);
      this.getUserList(page)
    },
    // 编辑按钮
    editHanlder (scope) {
      console.log('编辑按钮',scope.row);
    },
    // 删除按钮
    removeHanlder (scope) {
      // console.log('删除按钮',scope);

      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let id = scope.row.id
          userDel(id).then( res => {
            // console.log(res.data);
            let {meta} = res.data
            if(meta.status == 200){
              this.$message.success(meta.msg)
              this.getUserList(1,this.size)
            }else{
              this.$message.error(meta.msg)
              return
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });

    },
    // 新增用户
    addHanlder (){
      this.dialogFormVisible = true
    },
    // 存储信息
    storeUser () {
      console.log(this.form);
      userAdd(this.form).then(res => {
        let { meta,data } = res.data
        if (meta.status == 201) {
          this.$message.success(meta.msg);
          this.dialogFormVisible = false
          this.getUserList(1,this.size)
          this.form = {}
        }else{
          this.$message.error(meta.msg);
        }
       
      }).catch( error =>{
        
      })
    }
  }
}
</script>

<style>

</style>